package com.shop.cereshop.business.controller.feedback;

import com.shop.cereshop.business.annotation.NoRepeatSubmit;
import com.shop.cereshop.business.annotation.NoRepeatWebLog;
import com.shop.cereshop.business.service.feedback.FeedbackService;
import com.shop.cereshop.business.utils.ContextUtil;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.FeedbackConstant;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.param.feedback.SubmitFeedbackDTO;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户反馈控制器
 */
@RestController
@RequestMapping("/feedback")
@Api(tags = "用户反馈")
@RequiredArgsConstructor
public class FeedbackController {

  private final FeedbackService feedbackService;

  @PostMapping("/submit")
  @ApiOperation(value = "用户反馈(已用)")
  @NoRepeatSubmit
  @NoRepeatWebLog
  public Result<Object> submit(@RequestBody @Valid SubmitFeedbackDTO param) throws CoBusinessException {
    // 获取当前登录账户
    Long shopId = ContextUtil.getShopId();
    if (shopId == null) {
      return Result.failed(CoReturnFormat.USER_NOT_LOGIN);
    }
    param.setUserId(shopId);
    // 使用统一的用户类型定义
    param.setUserType(FeedbackConstant.UserType.SHOP);
    int row = feedbackService.submitFeedback(param);
    return row > 0 ? Result.success() : Result.failed(CoReturnFormat.FAIL);
  }
}